<?php
session_start();
if (isset($_SESSION["username"])) {
?>
  <?php require_once('common/header.php'); ?>
  <!--页面主要内容-->
  <main class="lyear-layout-content">
    <div class="container-fluid">
      <div class="row">
        <div class="col-lg-12">
          <div class="card">
            <div class="card-toolbar clearfix">
              <form class="pull-right search-bar" method="get" action="" role="form">
                <div class="input-group">
                  <div class="input-group-btn">
                    <input type="hidden" name="search" id="search" value="poster_name">
                    <button class="btn btn-default dropdown-toggle" id="search-btn" data-toggle="dropdown" type="button" aria-haspopup="true" aria-expanded="false">
                      海报名称 <span class="caret"></span>
                    </button>
                    <ul class="dropdown-menu">
                      <li> <a tabindex="-1" href="javascript:void(0)" data-field="poster_name">海报名称</a> </li>
                      <li> <a tabindex="-1" href="javascript:void(0)" data-field="poster_bz">备注说明</a> </li>
                    </ul>
                  </div>
                  <input type="text" class="form-control" value="" name="keyword" placeholder="请输入查询信息">
                </div>
              </form>
              <div class="toolbar-btn-action">
                <!--弹窗开始-->
         
                <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo"><i class="mdi mdi-plus"></i>添加海报</button>
                <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
                  <div class="modal-dialog" role="document">
                    <div class="modal-content">
                      <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title" id="exampleModalLabel">添加海报</h4>
                      </div>
                      <div class="modal-body">
                        <form action="controller/doAddhb.php" method="post" enctype="multipart/form-data">
                          <div class="form-group">
                            <label for="poster_name" class="control-label">海报名称：</label>
                            <input type="text" class="form-control" id="poster_name" name="poster_name" placeholder="海报名称" required>
                          </div>
                          <div class="form-group">
                            <label for="poster_url" class="control-label">海报图片：</label>
                            <input type="file" class="form-control" id="poster_url" name="poster_url" placeholder="海报图片" required>
                          </div>
                          <div class="form-group">
                            <label for="poster_bz" class="control-label">备注说明：</label>
                            <input type="text" class="form-control" id="poster_bz" name="poster_bz" placeholder="备注说明" >
                          </div>
                          <!--时间戳-->
                          <input type="hidden" name="cr_time" value="<?php echo time(); ?>">
                      </div>
                      <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">提交</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                      </div>
                        </form>
                    </div>
                  </div>
                </div>
                <!--这是弹窗结束-->
              </div>
            </div>
            <div class="card-body">
              <div class="table-responsive">
                <form name="form1" id="form1" method="post" action="controller/doUPhb.php">
                  <table class="table table-bordered">
                            <?php
                            include_once("../untils/conn.php");
                            mysqli_query($con, "set names utf8");
                            if ($con) {
                                if ($db) {
                                    // 获取搜索关键词和字段
                                    $keyword = $_GET['keyword'];
                                    $field = $_GET['search'];
                            
                                    // 构建查询语句
                                    $sql = "SELECT * FROM poster";
                                    if ($keyword != "") {
                                        $sql .= " WHERE $field LIKE '%$keyword%'";
                                    }

                                    // 获取数据总行数
                                    $data = mysqli_query($con, $sql);
                                    $maxrows = mysqli_num_rows($data);
                            
                                    // 计算总页数
                                    $page_size = 10; // 每页显示数
                                    if ($maxrows % $page_size == 0) {
                                        $maxpage = (int)($maxrows / $page_size);
                                    } else {
                                        $maxpage = (int)($maxrows / $page_size) + 1;
                                    }
                            
                                    // 获取当前页
                                    if (isset($_GET['curpage'])) {
                                        $page = $_GET['curpage'];
                                    } else {
                                        $page = 1;
                                    }
                            
                                    // 分段取出数据
                                    $start = $page_size * ($page - 1);
                                    $get_sql = "$sql ORDER BY id DESC LIMIT $start, $page_size";
                            
                                    // 获取数据显示
                                    $data = mysqli_query($con, $get_sql);
                                
                            ?>
                        <thead>
                          <tr>
                            <th>ID</th>
                            <th>海报名称</th>
                            <th>海报缩略图</th>
                            <th>备注说明</th>
                            <th>创建时间</th>
                           
                            <th>操作</th>
                          </tr>
                        </thead>
                        <tbody>
                          <?php
                          while ($row = mysqli_fetch_array($data)) {
                          ?>
                            <tr>
                              <td style="width:1%;">
                                <?php echo $row["id"] ?>
                              </td>
                              <td style="width:10%;"><?php echo $row["poster_name"] ?>
                              </td>
                              <td style="width:5%;" onclick="showPreview('<?php echo $row["poster_url"] ?>')" data-toggle="modal" data-target="#hbModal">
                                <img src="<?php echo $row["poster_url"] ?>" alt="" style="width:60px; height:60px;">
                              </td>
                              <td style="width:5%;"><?php echo $row["poster_bz"] ?>
                              </td>
                              <td style="width:5%;">
                              <?php  
                              $date = date('Y-m-d H:i:s', $row["cr_time"]);
                              echo $date; 
                              ?>
                              </td>
                             
                              <td style="width:15%;">
                                <button type="button" class="btn btn-info btn-sm" onclick="showPreview('<?php echo $row["poster_url"] ?>')" data-toggle="modal" data-target="#hbModal">
                                  预览
                                </button>
                                <!--<a href="#" class="btn btn-success btn-sm" data-toggle="modal" data-target="#xghbModal" data-whatever="@mdo">修改</a>-->
                                <a href="controller/deletehb.php?id=<?php echo $row['id'] ?>" onclick="return del()" class="btn btn-danger btn-sm">删除</a>
                              </td>
                              
                            </tr>
                          <?php
                          }
                          ?>
                        </tbody>

                        <tfoot>
                          <tr>
                            <td colspan="12" style="text-align: right;">
                          <?php
                          echo "<p>共 $maxpage 页&nbsp;&nbsp;";
                          echo "每页 $page_size 项&nbsp;&nbsp;";
                          //设置上一页
                          if ($page > 1) {
                            $prepage = $page - 1;
                            echo "<a href='?curpage=$prepage'>上一页</a>&nbsp;&nbsp;";
                          }
                          //设置下一页
                          if ($page < $maxpage) {
                            $nextpage = $page + 1;
                            echo "<a href='?curpage=$nextpage'>下一页</a>&nbsp;&nbsp;";
                          }
                          echo "&nbsp;&nbsp;第 $page 页</p>";
                        }
                      }
                          ?>
                            </td>
                          </tr>
                        </tfoot>
                  </table>
                </form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    </div>
    
    <!--预览模拟态-->
<div class="modal fade" id="hbModal" tabindex="-1" role="dialog" aria-labelledby="hbModalLabel">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">预览海报</h4>
      </div>
      <div class="modal-body" style= " display: flex;justify-content: center;align-items: center;">
        <img id="posterImage" src="" alt="" >
        <!--style="width:360px; height:760px; "-->
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
      </div>
    </div>
  </div>
</div>
<!--结束-->
 
  </main>
   <script src="../merchant/assets/js/jquery.min.js"></script>
    <script>
      $(document).ready(function() {
        $('.dropdown-menu a').click(function(e) {
          e.preventDefault();
          var field = $(this).data('field');
          var buttonText = $(this).text();
          $('#search').val(field);
          $('#search-btn').html(buttonText + ' <span class="caret"></span>');
        });
      });
   function showPoster(posterUrl) {
    var posterImage = document.getElementById('posterImage');
        posterImage.src = posterUrl;
      }
    
      function showPreview(posterUrl) {
        showPoster(posterUrl);
      }
    </script>
  <!--End 页面主要内容-->
  <?php require_once('common/footer.php'); ?>
<?php
} else {
  echo "<script>alert('您尚未登录，没有权限访问该页面');location.href='login.php';</script>";
}
